﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vtron.LEDCorrective.Core;
using Vtron.LEDCorrective.Infrastructure;

namespace Vtron.LEDCorrective.Pages
{
    /// <summary>
    /// ExpectedBrightColor.xaml 的交互逻辑
    /// </summary>
    public partial class ExpectedBrightColor : UserControl
    {
        public ExpectedBrightColor()
        {
            InitializeComponent();
        }

        private void showCIE_Btn_Click(object sender, RoutedEventArgs e)
        {
            CIEWindow window = new CIEWindow(this);
            window.ShowDialog();
        }

        private void submit_Btn_Click(object sender, RoutedEventArgs e)
        {
            DoubleMatrix targetxyY = CorrectiveEngine.Instance.TargetxyY;

            targetxyY[0, 0] = Double.Parse(redx_txt.Text);
            targetxyY[0, 1] = Double.Parse(redy_txt.Text);
            targetxyY[0, 2] = Double.Parse(redBright_txt.Text);
            targetxyY[1, 0] = Double.Parse(greenx_txt.Text);
            targetxyY[1, 1] = Double.Parse(greeny_txt.Text);
            targetxyY[1, 2] = Double.Parse(greenBright_txt.Text);
            targetxyY[2, 0] = Double.Parse(bluex_txt.Text);
            targetxyY[2, 1] = Double.Parse(bluey_txt.Text);
            targetxyY[2, 2] = Double.Parse(blueBright_txt.Text);

            ModernDialog.ShowMessage("设置期望亮色度成功！", "信息", MessageBoxButton.OK);
        }

        private void light_rb_Checked(object sender, RoutedEventArgs e)
        {
            CorrectiveEngine.Instance.CorrectiveMode = CorrectiveMode.Brightness;
            if (this.IsLoaded)
            {
                chroma_Grid.Visibility = Visibility.Collapsed;
            }
        }

        private void color_rb_Checked(object sender, RoutedEventArgs e)
        {
            InitTargetxyY(CorrectiveEngine.Instance.TargetxyYMeasure);
            CorrectiveEngine.Instance.CorrectiveMode = CorrectiveMode.Chroma;
            chroma_Grid.Visibility = Visibility.Visible;
        }

        private void measure_rb_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsLoaded)
            {
                InitTargetxyY(CorrectiveEngine.Instance.TargetxyYMeasure);
            }
        }

        private void sRGB_rb_Checked(object sender, RoutedEventArgs e)
        {
            InitTargetxyY(CorrectiveEngine.Instance.TargetxyYsRGB);
        }

        private void aRGB_rb_Checked(object sender, RoutedEventArgs e)
        {
            InitTargetxyY(CorrectiveEngine.Instance.TargetxyYaRGB);
        }

        public void InitTargetxyY(DoubleMatrix target)
        {
            redBright_txt.Text = target[0, 2].ToString();
            redx_txt.Text = target[0, 0].ToString();
            redy_txt.Text = target[0, 1].ToString();

            greenBright_txt.Text = target[1, 2].ToString();
            greenx_txt.Text = target[1, 0].ToString();
            greeny_txt.Text = target[1, 1].ToString();

            blueBright_txt.Text = target[2, 2].ToString();
            bluex_txt.Text = target[2, 0].ToString();
            bluey_txt.Text = target[2, 1].ToString();

            DoubleMatrix targetxyY = CorrectiveEngine.Instance.TargetxyY;
            targetxyY[0, 0] = target[0, 0];
            targetxyY[0, 1] = target[0, 1];
            targetxyY[1, 0] = target[1, 0];
            targetxyY[1, 1] = target[1, 1];
            targetxyY[2, 0] = target[2, 0];
            targetxyY[2, 1] = target[2, 1];
        }
    }
}
